package com.gzl.demo.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class CalendarUtil {

    /**
     * 获得当天零时零分零秒
     * @return
     */
    public static LocalDateTime initDateByDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date = calendar.getTime();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 获取当前星期几
     * @return
     */
    public static int getDayOfWeek() {
        try {
            int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0)
                w = 0;
            return weekDays[w];
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 当前时间是否在两个时段内
     * @param start
     * @param end
     * @return
     */
    public static boolean checkNowBelongs(String start, String end) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(start);
            endTime = df.parse(end);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return belongCalendar(now, beginTime, endTime);
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime,
                                         Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    //判断是否超过24小时
    public static boolean jisuan(String start1, String end1) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = sdf.parse(start1);
        Date end = sdf.parse(end1);
        long cha = end.getTime() - start.getTime();
        double result = cha * 1.0 / (1000 * 60 * 60);
        if(result <= 24){
            return false;
        }else{
            return true;
        }
    }

    // 分钟
    public static double hour(String date1, String date2) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = sdf.parse(date1);
        Date end = sdf.parse(date2);
        Long cha = end.getTime() - start.getTime();
        double result = cha * 1.0 / (1000 * 60 * 60);

        return result;
    }
    /**
     * 获取指定的日期最大时间
     * @return
     */
    public static LocalDateTime specifyDateByMax(LocalDateTime localDateTime) {
        return localDateTime.with(LocalTime.MAX);

    }

    /**
     * 计算两个时间段中相差多少秒
     * 开始时间小于结束时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    public static Long tiemDifference(LocalDateTime startTime,  LocalDateTime endTime) {
        try {

            Date startDateTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant());
            Date endDateTime = Date.from(endTime.atZone(ZoneId.systemDefault()).toInstant());
            //单位是毫秒
            long diff = endDateTime.getTime() - startDateTime.getTime();
            long second = diff / 1000;
            return second;
        } catch (Exception e) {
            log.error("tiemDifference , startTime= {}, endTime= {}",startTime,endTime);
            // 默认三个月
            Long second = Long.valueOf(86400 * 90);
            return second;
        }

    }


}
